import { Message } from "element-ui";

export default {
  bind(el, binding) {
    el.$value = binding.value;
    el.handler = () => {
      const textarea = document.createElement("textarea");
      textarea.readOnly = true;
      textarea.style.position = "absolute";
      textarea.style.left = "-9999px";
      textarea.value = el.$value;
      document.body.appendChild(textarea);
      textarea.select();
      textarea.setSelectionRange(0, textarea.value.length);
      const result = document.execCommand("Copy");
      if (result) {
        Message.success("复制成功");
      }
      document.body.removeChild(textarea);
    };
    el.addEventListener("click", el.handler);
  },
  componentUpdated(el, binding) {
    el.$value = binding.value;
  },
  unbind(el) {
    el.removeEventListener("click", el.handler);
  }
};
